# -*- coding: utf-8 -*-
from __future__ import unicode_literals

__author__ = 'zhengkc'

from django.shortcuts import render
from models import Item, Template, ItemCategory, Projects
from django.core import serializers
import json
import requests
from case_util.interface_test import md5


def project_show(request):
    project = Projects.objects.filter(project_name=u'项目一')
    item_category = ItemCategory.objects.all()
    template = Template.objects.all()
    items = Item.objects.all()
    # data = serializers.serialize('json', items)

    return render(request, 'project_plan.html', {'items': items, 'item_category': item_category, 'template': template})


def lesson_plan(request):
    host = ''
    post_url = ''
    env = request.POST.get('env')
    username = request.POST.get('username')
    if env == 'dev':
        host = 'test-lms3.hfjy.com'
        post_url = 'http://test-lms3.hfjy.com:1080/user/login'
    elif env == 'test':
        host = 'test-s3.hfjy.com'
        post_url = 'http://test-s3.hfjy.com/user/login'
    elif env == 'outline':
        host = 'i2.hfjy.com'
        post_url = 'http://i2.hfjy.com/user/login'

    header_login = {
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding': 'gzip, deflate, sdch',
        'Accept-Language': 'zh-CN,zh;q=0.8',
        'Connection': 'keep-alive',
        'Host': host,
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest'
    }

    u = md5(username)
    p = md5('123qweasd')
    password = md5(u + p)
    data = {
        'username': username,
        'password': password
    }
    r = requests.post(post_url, headers=header_login, data=data)
    # r.encoding = 'utf-8'
    res = r.content.decode('utf-8')
    try:
        name = eval(res)["data"]["sessionUser"]["name"]
        access_token = eval(res)["data"]["access_token"]
        userId = eval(res)["data"]["sessionUser"]["userId"]
        passwordState = eval(res)["data"]["sessionUser"]["passwordState"]
        roleCode = eval(res)["data"]["sessionUser"]["role"]["roleCode"]
        roleId = eval(res)["data"]["sessionUser"]["role"]["roleId"]

        return render(request, 'lesson.html',
                      {'access_token': access_token, 'name': name, 'passwordState': passwordState, 'userId': userId,
                       'roleCode': roleCode,
                       'roleId': roleId, 'env': env})
    except:
        return render(request, 'lesson.html', {'error': '账号或密码不正确'})


        # res = eval(r.content)['data']['access_token']
        # header_main = {
        #     'Accept': 'application/json, text/javascript, */*; q=0.01',
        #     'Accept-Encoding': 'gzip, deflate, sdch',
        #     'Accept-Language': 'zh-CN,zh;q=0.8',
        #     'Connection': 'keep-alive',
        #     'Authorization': res,
        #     'Host': 'test-lms3.hfjy.com',
        #     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
        #     'X-Requested-With': 'XMLHttpRequest'
        # }
        #
        # r = requests.post('http://test-lms3.hfjy.com:1080/resource/index.html#!/lessonplan/teacher', headers=header_main)
        # print r.content
        # requests.post()


def index(request):
    return render(request, 'qa_index.html')


def lesson_choice(request):
    return render(request, 'lesson_choice.html')


def change_item_plan(request):
    item_plan = request.POST.get('item_plan')
    item_id = request.POST.get('itemid')
    item = Item.objects.get(id=item_id)
    item.item_plan = item_plan
    item.save()
    return render(request, 'qa_index.html')



